/**
 * Copyright (c) 2013-1-6 下午06:07:17 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.wkm.task;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wkm.task.factory.SchedulerFactoryUtil;
import org.wkm.task.job.CloseSystemJob;
import org.wkm.task.job.Heartbeat;

/**
 * @author WKM
 * Tasks.java history
 * 1.WKM - 2013-1-6 - Create File
 */
public class Tasks extends Thread {

	/** 日志信息 **/
	private static Logger logger = LoggerFactory.getLogger(CloseSystemJob.class);
	
	private Calendar currentTime = null;
	
	private SchedulerFactoryUtil sfu = new SchedulerFactoryUtil();
	
	public Tasks(){
	}
	
	/**
	 * 开始任务
	 */
	public void run() {
		this.setName("Thread-Monitor:" + getClass().getName());
		logger.info("启动监控中心...");
		if(currentTime == null){
			logger.info("没有对应的时间,请先确定时间！");
			return ;
		}
		if(sfu != null){
			sfu.stop();
		}
		sfu.addJob(Heartbeat.class, "*/10 * * * * ?");
		String cron = "* " + currentTime.get(Calendar.MINUTE)+ " " + currentTime.get(Calendar.HOUR_OF_DAY) + " * * ?";
		
		logger.info("在时间" + new SimpleDateFormat("yyyyMMddHHmm").format(currentTime.getTime()) + "关闭计算机!");
		sfu.addJob(CloseSystemJob.class, cron);
		sfu.bootup();
	}

	public Calendar getCurrentTime() {
		return currentTime;
	}

	public void setCurrentTime(Calendar currentTime) {
		this.currentTime = currentTime;
	}
	
	/**
	 * 停止任务
	 */
	public void stopTask(){
		if(sfu != null){
			sfu.stop();
		}
	}
	
}
